<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Acesso\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
use Zend\Authentication\AuthenticationService;

class LoginController extends AbstractActionController
{

    protected $loginTable;

    public function logarAction()
    {
        /* $request   = $this->getRequest();
          $viewmodel = new ViewModel();
          //disable layout if request by Ajax
          $viewmodel->setTerminal( $request->isXmlHttpRequest() );
          $viewmodel->setVariables( array(
          'is_xmlhttprequest' => 1
          ) );
          return $viewmodel; */
        $usuario = $this->getRequest()->getPost( 'usuario', null );
        $senha   = $this->getRequest()->getPost( 'senha', null );
        $form    = new \Acesso\Form\Login();
        $request = $this->getRequest();

        if ($request->isPost()) {
            $login = new \Acesso\Filter\Login();
            $form->setInputFilter( $login->getInputFilter() );
            $form->setData( $request->getPost() );

            if ($form->isValid()) {
                $data = $login->exchangeArray( $form->getData() );
            }
        }
        /* $login = new \Acesso\Model\Business\Acesso();
          //$login->verificaUsuario($usuario, $senha);
          $table = $this->getLoginTable()->fetchAll(); */


        $sm = $this->getServiceLocator();
        $db = $sm->get( 'db' );

        $business = new \Acesso\Model\Business\Usuario( $db );

        $business->autentica( $usuario, $senha );

        $auth = new AuthenticationService();

        //$table->fetchAll();
        /* $json = new JsonModel( array(
          'type'    => 'success',
          'message' => $auth->getIdentity()) ); */
        $json = new JsonModel( array(
            'type' => 'success') );
        return $json;

    }

}
